package org.flexharmony.eclipse.properties.classinclusion;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.flexharmony.eclipse.HarmonyPlugin;
import org.flexharmony.eclipse.domain.IncludedSourceFolder;

public class AddSourceFoldersDialog extends Dialog implements SelectionListener
{
  private Button okButton;
  private IProject project;
  private List<IncludedSourceFolder> currentFolders;
  private boolean add;
  private List<IPath> selectedFolders;
  private Table table;
  
  public AddSourceFoldersDialog(Shell parentShell, IProject project, List<IncludedSourceFolder> currentFolders)
  {
    super(parentShell);
    this.project = project;
    this.currentFolders = currentFolders;
  }

  public boolean isAdd()
  {
    return add;
  }

  public List<IPath> getSelectedFolders()
  {
    return selectedFolders;
  }

  public void widgetDefaultSelected(SelectionEvent evt)
  {
    buttonPressed(IDialogConstants.OK_ID);
  }

  public void widgetSelected(SelectionEvent e)
  {
    okButton.setEnabled(table.getSelection().length > 0);
  }
  
  protected void configureShell(Shell shell)
  {
    super.configureShell(shell);
    shell.setText("Select Java Source Folders");
  }

  protected Control createDialogArea(Composite parent)
  {
    Composite composite = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData();
    gd.widthHint = 400;
    gd.heightHint = 200;
    composite.setLayoutData(gd);
    
    
    table = new Table(composite, SWT.MULTI);
    table.addSelectionListener(this);
    GridData tableData = new GridData();
    tableData.grabExcessVerticalSpace = true;
    tableData.grabExcessHorizontalSpace = true;
    tableData.verticalAlignment = GridData.FILL;
    tableData.horizontalAlignment = GridData.FILL;
    table.setLayoutData(tableData);

    TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
    tableColumn1.setWidth(250);
    tableColumn1.setText("Java Source Files");
   
    IJavaProject javaProject = JavaCore.create(project);
    try
    {
      IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
      for (IClasspathEntry classpathEntry : classpathEntries)
      {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
        {
          IPath path = classpathEntry.getPath().removeFirstSegments(1); // project relative
          
          boolean foundPath = false;
          for (IncludedSourceFolder folder : currentFolders)
          {
            if (folder.folderPath.equals(path))
            {
              foundPath = true;
              break;
            }
          }
          if (!foundPath)
          {
            TableItem ti = new TableItem(table, SWT.NONE);
            ti.setData(path);
            String pathStr = path.toString();
            ti.setText(" " + pathStr);
            ti.setImage(HarmonyPlugin.getImage("/icons/sourceFolder.gif"));
          }
        }
      }
    }
    catch (JavaModelException jmexc)
    {}
    
    return composite;
  }

  protected void createButtonsForButtonBar(Composite parent)
  {
    okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }

  protected void buttonPressed(int buttonId) 
  {
    switch (buttonId)
    {
      case IDialogConstants.OK_ID:
        TableItem[] selectedItems = table.getSelection();
        if (selectedItems.length > 0)
        {
          add = true;
          selectedFolders = new ArrayList<IPath>();
          for (TableItem selectedItem : selectedItems)
          {
            selectedFolders.add((IPath)selectedItem.getData());
          }
        }
        close();
        break;
        
      default:
        super.buttonPressed(buttonId);
        break;
    }
  }

}
